package com.supertange.miniapp.wepet.common;

import com.supertange.miniapp.wepet.common.utils.MyException;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalException {
    private final Logger logger= LoggerFactory.getLogger(this.getClass());
    /**
     * 所有异常报错
     * @param exception
     * @return
     */
    @ExceptionHandler(value= Exception.class)
    public AjaxResult allExceptionHandler(Exception exception) {
        exception.printStackTrace();
        logger.error("报错："+exception.getLocalizedMessage());
        return AjaxResult.errorMsg("请求异常或服务器异常，请稍后再试！");
    }

    /**
     * 微信异常处理
     * @return
     */
    @ExceptionHandler(value= WxErrorException.class)
    public AjaxResult wxExceptionHandler(WxErrorException wxException) {
        wxException.printStackTrace();
        logger.error("报错："+wxException.getLocalizedMessage());
        return AjaxResult.errorMsg("微信系统服务异常，请稍后再试！");
    }

    /**
     * 自定义异常处理类
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    public AjaxResult myExceptionHandler(RuntimeException e) {
        logger.error(e.getMessage(), e);
        return AjaxResult.errorMsg(e.getMessage());
    }
}
